package com.xyy.saas.payment.cores.enums;

import java.util.Arrays;
import java.util.Map;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

/**
 * @Author: wuqiongda
 * @Date: 2019/12/23 19:10
 */
public enum PayModeEnum {

    /**
     * 小程序支付
     */
    MINI("mini"),
    /**
     * APP支付
     */
    APP("app"),
    /**
     * 二维码支付
     */
    QR("qr"),
    /**
     * PC端支付
     */
    PC("pc"),
    /**
     * 公众号
     */
    PUB("pub"),
    /**
     * H5 手机网站
     */
    H5("h5"),
    /**
     * @Description 
     * @Param 银联在线网关
     * @return 
     * @Author User
     * @Date 2021/6/28 下午2:23
     **/
    ONLINE_GW("online_gw"),
    /**
     * @Description 
     * @Param 银联手机支付
     * @return 
     * @Author User
     * @Date 2021/6/28 下午3:05
     **/
    APP_WAP("app_wap"),
    /**
     * 京东支付
     */
    JD_CARD(ChannelEnum.JD_CARD.getCode());

    private String mode;

    /**
     * 平安支付方式 -> 支付平台支付方式
     * @param pinganPayMode
     * @return
     */
    public static String convertPingan(String pinganPayMode, String pinganChannel, String prePayOrderExtra) {
        if(pinganPayMode == null) {
            throw new RuntimeException("pinganPayMode is null");
        }

        if("AlipayJsapi".equals(pinganPayMode) || "WeixinProgram".equals(pinganPayMode)) {
            return MINI.getMode();
        }
        else if("WeixinJsapi".equals(PUB.getMode())) {
            return PUB.getMode();
        }

        if(ChannelEnum.PINGAN_CREDIT.getCode().equals(pinganChannel)) {
            if(prePayOrderExtra.contains("frontSkipUrl")) {
                return PayModeEnum.PC.getMode();
            }
            else {
                return PayModeEnum.APP.getMode();
            }
        }

        throw new RuntimeException("pinganPayMode is null");
    }

    private static Map<String,PayModeEnum> code2Enum;

    static {
        code2Enum = Arrays.asList(values()).stream().collect(Collectors.toMap(PayModeEnum::getMode, UnaryOperator.identity()));
    }

    public static PayModeEnum getEnum(String code){
        return code2Enum.get(code);
    }

    PayModeEnum(String mode){
        this.mode = mode;
    }

    public String getMode() {
        return mode;
    }
}
